package com.dropTables.TakingNote;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {
	
	private Button confirm_button;			//User presses when info is entered
	private Button register_button;			//User presses if they don't have an account
	private EditText username_enter;		//User enters username
	private EditText password_enter;		//User enters password
	
	
	static final int ACTIVITY_REGISTER = 1;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);												//Get appropriate field
		
		confirm_button = (Button) findViewById(R.id.login_confirm_button);			//Get buttons and fields using their id's
		register_button = (Button) findViewById(R.id.login_register_button);
		username_enter = (EditText) findViewById(R.id.login_username_input);
		password_enter = (EditText) findViewById(R.id.login_password_input);
		
		
		
		
		
		confirm_button.setOnClickListener(new View.OnClickListener() {				//If confirm button clicked
			@Override
			public void onClick(View v) {
				login();															//Call login function
				
			}
		});
		
		register_button.setOnClickListener(new View.OnClickListener() {				//If register button clicked
			@Override
			public void onClick(View v) {
				Intent i = new Intent(v.getContext(), RegisterActivity.class);		//Start register activity
				startActivityForResult(i, ACTIVITY_REGISTER);
				
			}
		});
	}
	
	private void login(){
		String un, pass, response;
		pass = password_enter.getText().toString();						//Get username and login from their respective fields
		un = username_enter.getText().toString();
		char res1 = 'x';
		response = "";
		
		try {															//Try calling the login function in HttpComm
			response = HttpComm.executeHttplogin(un, pass);
		} catch (Exception e) {
			e.printStackTrace();
		}
		res1 = response.charAt(0);
		if(res1 == '1'){												//If login successful
            FileOutputStream os = null;
            try {
                    String towrite = un+"\n"+pass;								//Store username and password to file
                    os = openFileOutput("TakingNotelogininfo", MODE_PRIVATE);
                    os.write(towrite.getBytes());
                    os.close();
                    Toast.makeText(this, "Login successful!", 1).show();		//Tell user they logged in successfully
                    HttpComm.readLoginInfo();
                    
                    finish();
                    startActivity(new Intent().setClass(this, NormalNoteActivity.class));
            } 
            catch (FileNotFoundException e) {
                    e.printStackTrace();
            } 
            catch (IOException e) {
                    e.printStackTrace();
            }
    }
		else
			Toast.makeText(this, "Bad username or password.", 1).show();		//Tell user they don't exist
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent){
		super.onActivityResult(requestCode, resultCode, intent);
		
		
		switch(requestCode){
		case ACTIVITY_REGISTER:							//Give message on result of register
			if(resultCode == RESULT_OK){
				Toast.makeText(this, "REGISTERED!", 1).show();				
			}
			else{
				Toast.makeText(this, "Canceled :(", 1).show();
			}
			break;
		}
	}
	
	

}
